<template>
	<view class="once-header">
		<fui-list>
			<fui-list-cell>
				<text>退款金额</text>
				<text class="fui-text__explain" style="color: #f25014;">￥{{money}}</text>
			</fui-list-cell>
			<view @click="showModal">
				<fui-list-cell>
					<text>退款原因</text>
					<text class="fui-text__explain" v-if="message === ''">请选择 ></text>
					<text class="fui-text__explain" v-else>{{ message }}</text>
				</fui-list-cell>
			</view>

		</fui-list>
		<view style="margin-left: 18px;margin-top: 10px;margin-bottom: 10px;">
			退款说明
		</view>
		<fui-textarea placeholder="选填原因" maxlength="200" isCounter="true"></fui-textarea>

		<view v-if="isModalVisible" class="modal">
			<view class="modal-content">
				<view class="modal-header">
					<text>请选择原因</text>
				</view>
				<view class="modal-body">
					<view v-for="(bank, index) in banks" :key="index" @click="selectBank(index)" class="bank-option">
						<radio color="#333" :value="bank.name" :checked="selectedBank.value === bank.name" />
						<text>{{ bank.name }}</text>
					</view>
				</view>
				<view class="modal-footer">
					<button @click="confirmSelection" class="confirm-button">确定</button>
				</view>
			</view>
		</view>
	</view>
	<view class="footer">
		<view class="refund-amount">退款金额 <text class="amount">¥{{money}}</text></view>
		<button class="submit-button" @click="quxiaodingdan">提交</button>
	</view>
</template>

<script setup>
	import {
		url
	} from '@/utils/request.js';
	import {
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app';

	const isModalVisible = ref(false);
	const banks = ref([{
			name: '计划有变没时间消费'
		},
		{
			name: '买多了/买错了'
		},
		{
			name: '后悔了，不想要'
		},
		{
			name: '与商家协同一致退款'
		},
		{
			name: '拍错/多拍/不喜欢'
		},
	]);
	const orderId = ref('')
	const message = ref('');
	const messages = ref('');
	const selectedBank = ref('');
	const money = ref('');
	onLoad((options) => {
		if (options.xinxi) {
			const xinxi = JSON.parse(decodeURIComponent(options.xinxi));
			money.value = xinxi.money
			orderId.value = xinxi.orderId
			console.log('orderId:', xinxi.orderId);
			console.log('createTime:', xinxi.createTime);
			console.log('money:', xinxi.money);
			console.log('nums:', xinxi.nums);
			console.log('totalNums:', xinxi.totalNums);
		}
	});

	const showModal = () => {
		isModalVisible.value = true;
	};
	const hideModal = () => {
		isModalVisible.value = false;
	};
	const selectBank = (index) => {
		const bank = banks.value[index];
		selectedBank.value = bank.name;
	};
	const confirmSelection = () => {
		message.value = selectedBank.value;
		console.log('Selected reason:', selectedBank.value);
		hideModal();
	};
	const quxiaodingdan = () => {

		if (message.value === '') {
			uni.showModal({
				title: '提示',
				content: '请您选择退款原因',
				success: function(res) {
					if (res.confirm) {

					} else {

					}
				}
			})
		}
    else{
		uni.request({
			url: url + '/placeAnOrder/userCancelOrder',
			method: 'GET',
			data: {
				orderId: orderId.value
			},
			success: (res) => {
				messages.value = (res.data.message)
				if (messages.value === '骑手已经开始派送无法取消订单') {
					uni.showModal({
						title: '抱歉',
						content: '骑手已经开始派送无法取消订单，无法取消',
						success: function(res) {
							if (res.confirm) {
								
							} else {
							
							}
						}
					})
				} else if (messages.value === '取消成功') {
					uni.showModal({
						title: '提示',
						content: messages.value,
						success: function(res) {
							if (res.confirm) {
							uni.switchTab({
								url:'../index/index'
							})
							} else {
								
							}
						}
					})
				}
				console.log(res.data.message)
			}
		});
	}
		
	}
</script>


<style>
	.footer {
		width: 100%;
		height: 62px;
		background: #ffffff;
		position: fixed;
		bottom: 0;
		display: flex;
		align-items: center;
		padding: 0 20px;
		box-sizing: border-box;
	}

	.refund-amount {

		font-size: 18px;
	}

	.amount {
		color: #f25014;
		font-weight: bold;
	}

	.submit-button {
		background: #333;
		color: white;
		width: 126px;
		margin-right: 0px;
	}

	.once-header {
		background-color: #ffffff;
		margin: 0 10px;
	}

	.open-modal-button {
		background-color: #FFCC00;
		color: #fff;
		border: none;
		padding: 10px 20px;
		border-radius: 5px;
		cursor: pointer;
	}

	.modal {
		z-index: 1000000;
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.modal-content {
		background-color: #fff;
		border-radius: 10px;
		width: 80%;
		padding: 20px;
	}

	.modal-header {
		text-align: center;
		font-size: 18px;
		margin-bottom: 10px;
	}

	.modal-body {
		padding: 0 10px;
	}

	.bank-option {
		display: flex;
		align-items: center;
		padding: 10px 0;
		border-bottom: 1px solid #f0f0f0;
	}

	.confirm-button {
		background-color: #333;
		color: #fff;
		border: none;
		padding: 0px 20px;
		margin: 20px auto 0;
		border-radius: 5px;
		display: block;
		cursor: pointer;
	}
</style>